package top.humbleyuan.dfs;

import top.humbleyuan.datastructure.tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author HumbleYuan
 * @Date 2020/5/19 19:38
 * @Des 计算树高
 */
public class LeetCode_104 {
    public static void main(String[] args) {
        // 循环迭代使用队列
        m1();

        // 递归
        m2();
    }

    public static void m1() {
        TreeNode head = new TreeNode(2);
        head.formTree();

        if(head == null) {
            System.out.println(0);
        }
        // 借助队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(head);

        int depth = 0;

        while (!queue.isEmpty()) {

            Queue<TreeNode> temp = new LinkedList<>();
            for (TreeNode t : queue) {
                if (t.left != null) {
                    temp.offer(t.left);
                }

                if(t.right != null) {
                    temp.offer(t.right);
                }
            }
            queue = temp;
            depth++;
        }
        System.out.println(depth);
    }

    public static void m2() {
        TreeNode head = new TreeNode(2);
        head.formTree();

        // 方法二，递归
        System.out.println(recursion(head));
    }

    // dfs
    public static int recursion(TreeNode t) {
        if(t == null) {
            return 0;
        }

        int leftDepth = recursion(t.left);
        int rightDepth = recursion(t.right);

        return Math.max(leftDepth, rightDepth) + 1;
    }
}
